package com.jams.goodox.activity.home;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.jams.goodox.BaseFragment;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.mine.CourseActivity;
import com.jams.goodox.activity.mine.address.MineAddressActivity;
import com.jams.goodox.activity.mine.buy.MineBuyActivity;
import com.jams.goodox.activity.mine.download.MyDownloadActivity;
import com.jams.goodox.activity.mine.message.MineMessageActivity;
import com.jams.goodox.activity.mine.ox.OxBi;
import com.jams.goodox.activity.mine.personal.MyInfoActivity;
import com.jams.goodox.activity.mine.settings.MineFeedback;
import com.jams.goodox.activity.mine.settings.MineSettingsActivity;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.ProtocolManagers;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.StringUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * 个人主页
 */
public class MineTeacherFragment extends BaseFragment {

    private static final String TAG = "MineTeacherFragment";
    private static final String ARG_PARAM1 = "param1";
    @Bind(R.id.sdv_enterprise_portrait)
    SimpleDraweeView sdvEnterprisePortrait;
    @Bind(R.id.tv_mine_name)
    TextView tvMineName;

    @Bind(R.id.tv_connect_company_status)
    TextView tv_connect_company_status;

    View rootView;
    @Bind(R.id.tx_message_number)
    TextView txMessageNumber;
    @Bind(R.id.tx_buy_number)
    TextView txBuyNumber;
    @Bind(R.id.tx_quest_number)
    TextView txQuestNumber;

    private Intent in;
    private MineInfo mineInfo;

    public static MineTeacherFragment newInstance(String param1) {
        MineTeacherFragment fragment = new MineTeacherFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.mine_teacher_fragment, container, false);
        }
        ButterKnife.bind(this, rootView);
        tvMineName.setText(SePref.getCommunityName());
        tv_connect_company_status.setText(SePref.getUserPhone());

        //getData();
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        enqueue(createCallService(MineService.class).queueUserInfo(ProtocolManagers.getDefaultMap()), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call call, BaseEntity result) {
                Log.d(TAG,"result == " + result.getResponseData());
                if (TextUtils.isEmpty(result.getResponseData())) return;
                mineInfo = new Gson().fromJson(result.getResponseData(), MineInfo.class);
                sdvEnterprisePortrait.setImageURI(Uri.parse(StringUtils.newString(mineInfo.getImgPath())));
                tvMineName.setText(mineInfo.getNickName());
                tv_connect_company_status.setText(mineInfo.getCity());
                if (mineInfo.getMessageNum() > 0) {
                    txMessageNumber.setText(String.valueOf(mineInfo.getMessageNum()));
                }
                if (mineInfo.getCourseNum() > 0) {
                    txBuyNumber.setText(String.valueOf(mineInfo.getCity()));
                }
                if (mineInfo.getQuestionNum() > 0) {
                    txQuestNumber.setText(String.valueOf(mineInfo.getQuestionNum()));
                }
                SePref.putCommunityName(mineInfo.coinCount);
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tx_edit_info, R.id.rela_feed, R.id.rela_message, R.id.rela_download, R.id.rela_buy, R.id.rela_question,R.id.rela_oxbi, R.id.img_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tx_edit_info:
                Bundle bundle=new Bundle();
                bundle.putSerializable("mineInfo",mineInfo);
                qStartActivity(MyInfoActivity.class,bundle);
                break;
            case R.id.rl_address:
                in = new Intent(getActivity(), MineAddressActivity.class);
                startActivity(in);
                break;
            case R.id.rela_feed:
                qStartActivity(MineFeedback.class);
                break;
            case R.id.rela_question:
                qStartActivity(CourseActivity.class);
                break;
            case R.id.rela_message:
                qStartActivity(MineMessageActivity.class);
                break;
            case R.id.rela_download:
                qStartActivity(MyDownloadActivity.class);
                break;
            case R.id.rela_buy:
                qStartActivity(MineBuyActivity.class);
                break;
            case R.id.img_setting:
                qStartActivity(MineSettingsActivity.class);
                break;
            case R.id.rela_oxbi:
                qStartActivity(OxBi.class);
                break;
        }
    }

}
